// index.js
<<<<<<< HEAD
const request = require('../../utils/request.js');

Page({
  data: {
    banners: [
      { image: '../../images/gif1.gif' },
      { image: '../../images/gif2.gif' },
      { image: '../../images/gif3.gif' }
    ],
    merchants: [],
    promotions: []
  },

  onLoad: function() {
    const app = getApp();
    // 监听全局状态变更事件
    app.on('merchantStatusChanged', this.reloadData.bind(this));
    this.loadPromotions();
    this.loadMerchants();
  },
  onUnload: function() {
    // 页面卸载时取消事件监听（避免内存泄漏）
    const app = getApp();
    app.off('merchantStatusChanged', this.reloadData.bind(this));
  },
  reloadData: function() {
    this.loadMerchants(); // 重新获取商家列表
    this.loadPromotions();// 重新获取优惠活动
  },
  // 加载商家列表
 loadMerchants: function() {
  request.get('/api/merchant/list').then(res => {
    if (res.success) {
      this.setData({
        merchants: res.data.map(merchant => ({
          ...merchant,
          statusText: merchant.status === 1 ? '营业中' : '暂停营业',
          statusColor: merchant.status === 1 ? '#4CAF50' : '#FF4444'
        }))
      });
    } else {
      wx.showToast({
        title: '获取商家列表失败',
        icon: 'none'
      });
    }
  });
},

  // 加载优惠活动
  loadPromotions: function() {
    const app = getApp();
    const merchantId = app.globalData.selectedMerchant.merchant_id;
    
    request.get('/api/promotion/list', { merchantId }).then(res => {
      if (res.success) {
        // 格式化日期显示
        const promotions = res.data.map(item => {
          item.start_time = item.start_time.substring(0, 10);
          item.end_time = item.end_time.substring(0, 10);
          return item;
        });
        this.setData({ promotions });
      }
    });
  },

  // 选择商家
  selectMerchant: function(e) {
    const merchant = e.currentTarget.dataset.merchant;
    const app = getApp();
    
    // 更新全局选中的商家
    app.globalData.selectedMerchant = merchant;
    
    // 跳转到菜单页
    wx.switchTab({
      url: '/pages/menu/menu'
    });
  }
});
=======
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  
  
  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
})
>>>>>>> c88a7d9722a90947456bbbcbbcbc83948938109e
